<?php

namespace App\Controller;

use app\BaseController;

class Relevance extends BaseController
{
    public function __construct(\think\App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        $this->UserModel = new \app\common\model\User;
        $this->RankModel = new \app\common\model\Rank;
        $this->RelevanceModel = new \app\common\model\Relevance;
        $this->ProjectModel = new \app\common\model\Project;
    }

    // 获取账号-项目关联列表
    public function list()
    {
        if ($this->request->isGet()) {
            $userid = $this->request->param('userid', 0, 'trim');

            $list = $this->RelevanceModel->where('userid', $userid)->select();
            return json(success_ful('项目成员列表', $list));
        }
    }

    // 获取关联详情
    public function info()
    {
        if ($this->request->isGet()) {
            $proid = $this->request->param('proid', 0, 'trim');
            $userid = $this->request->param('userid', 0, 'trim');

            $userInfo = $this->UserModel->where('id', $userid)->find();
            if (empty($userInfo)) {
                return json(error_ful('账号不存在'));
            }
            $proInfo = $this->ProjectModel->where('id', $proid)->find();
            if (empty($proInfo)) {
                return json(error_ful('项目不存在'));
            }

            $where = [
                'proid' => $proid,
                'userid' => $userid
            ];
            $list = $this->RelevanceModel->where($where)->find();
            if (empty($list)) {
                return json(error_ful('未关联文件夹'));
            }

            return json(success_ful('返回关联权限', $list));
        }
    }

    // 新增关联
    public function add()
    {
        if ($this->request->isGet()) {
            $parentid = $this->request->param('parentid', 0, 'trim');
            $userid = $this->request->param('userid', 0, 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $auth = $this->request->param('auth', '1', 'trim');

            // 检查是否为有权限
            $authValue = $this->RelevanceModel->where(['proid' => $proid, 'userid' => $parentid])->value('auth');
            if ($authValue * 1 < 4) {
                return json(error_ful('当前账号无权限'));
            }

            $where = [
                'proid' => $proid,
                'userid' => $userid
            ];
            $relevanceInfo = $this->RelevanceModel->where($where)->find();
            if (!empty($relevanceInfo)) {
                return json(error_ful('该账号已加入'));
            }

            $userInfo = $this->UserModel->where('id', $userid)->find();
            if (empty($userInfo)) {
                return json(error_ful('该账号不存在'));
            }
            $proInfo = $this->ProjectModel->where('id', $proid)->find();
            if (empty($proInfo)) {
                return json(error_ful('项目不存在'));
            }

            $data = [
                'userid' => $userid,
                'proid' => $proid,
                'auth' => $auth
            ];

            $status = $this->RelevanceModel->save($data);
            if ($status === false) {
                return json(error_ful('添加成员失败'));
            } else {
                return json(success_ful('添加成功'));
            }
        }
    }

    // 修改项目成员权限
    public function update()
    {
        if ($this->request->isGet()) {
            $parentid = $this->request->param('parentid', 0, 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $userid = $this->request->param('userid', 0, 'trim');
            $auth = $this->request->param('auth', '', 'trim');

            // 检查文件夹是否存在
            $proInfo = $this->ProjectModel->where('id', $proid)->find();
            if (empty($proInfo)) {
                return json(error_ful("文件夹不存在"));
            }

            // 检查修改的成员是否在当前文件夹
            $relevanceInfo = $this->RelevanceModel->where(['userid' => $userid, 'proid' => $proid])->find();
            if (empty($relevanceInfo)) {
                return json(error_ful("成员不存在"));
            }
            // 检查当前账号在文件夹的权限
            $pAuth = $this->RelevanceModel->where(['userid' => $parentid, 'proid' => $proid])->value('auth');
            if ($pAuth !== '4') {
                return json(error_ful('当前账号无权限'));
            }

            // 判断修改的账号是否为超管
            $userInfo = $this->UserModel->where('id',$userid)->find();
            if(empty($userInfo)){
                return json(error_ful('账号不存在'));
            }
            $weigh = $this->RankModel->where('id',$userInfo['rankid'])->value('weigh');
            if($weigh == '99'){
                return json(error_ful('超管权限禁止修改'));
            }


            // 检查权限是否修改
            $status = $this->RelevanceModel->where('id',$relevanceInfo['id'])->update(['auth' => $auth]);
            if ($status === false) {
                return json(error_ful('修改权限失败'));
            } elseif ($status == 0) {
                return json(error_ful('无任何修改'));
            } else {
                return json(success_ful('修改成功'));
            }
        }
    }

    // 删除关联 移除项目成员
    public function delete()
    {
        if ($this->request->isGet()) {
            $parentid = $this->request->param('parentid', 0, 'trim');
            $userid = $this->request->param('userid', 0, 'trim');
            $proid = $this->request->param('proid', 0, 'trim');

            // 检查文件夹是否存在
            $proInfo = $this->ProjectModel->where('id', $proid)->find();
            if (empty($proInfo)) {
                return json(error_ful("文件夹不存在"));
            }

            // 检查修改的成员是否在当前文件夹
            $relevanceInfo = $this->RelevanceModel->where(['userid' => $userid, 'proid' => $proid])->find();
            if (empty($relevanceInfo)) {
                return json(error_ful("成员不存在"));
            }
            // 检查当前账号在文件夹的权限
            $pAuth = $this->RelevanceModel->where(['userid' => $parentid, 'proid' => $proid])->value('auth');
            if ($pAuth !== '4') {
                return json(error_ful('当前账号无权限'));
            }

            // 判断修改的账号是否为超管
            $userInfo = $this->UserModel->where('id',$userid)->find();
            if(empty($userInfo)){
                return json(error_ful('账号不存在'));
            }
            $weigh = $this->RankModel->where('id',$userInfo['rankid'])->value('weigh');
            if($weigh == '99'){
                return json(error_ful('超管权限禁止删除'));
            }

            $status = $this->RelevanceModel->where('id',$relevanceInfo['id'])->delete();
            if($status === false){
                return json(error_ful('删除失败'));
            }elseif($status == 0){
                return json(error_ful('无任何删除'));
            }else{
                return json(success_ful('删除成功'));
            }
        }
    }
}